
#include "TreeView.h"

CTreeView::CTreeView()
{
}

CTreeView::~CTreeView()
{
}

void CTreeView::PreCreateWindow(CREATEWINDOWPARAM& param)
{
        param.dwStyle                   = WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_HASLINES | WS_VISIBLE | WS_MAXIMIZEBOX;
        param.WndClass.lpszClassName    = WC_TREEVIEW;
        param.WndClass.hbrBackground    = (HBRUSH)GetStockObject(WHITE_BRUSH);
}

HTREEITEM CTreeView::AddItemToTree(LPCTSTR szItem, int nLevel, int nImage)
{
    TVITEM  tvi;
    TVINSERTSTRUCT  tvins;
    static HTREEITEM hPrev = (HTREEITEM)TVI_FIRST;
    static HTREEITEM hPrevRootItem = NULL;
    static HTREEITEM hPrevLev2Item = NULL;
    
    tvi.mask = TVIF_TEXT | TVIF_PARAM | TVIF_SELECTEDIMAGE | TVIF_IMAGE;

    tvi.pszText = (LPSTR)szItem;
    tvi.cchTextMax = lstrlen(szItem);
    tvi.iImage = nImage;
    tvi.iSelectedImage = nImage;
    tvi.lParam = (LPARAM)nLevel;

    tvins.item = tvi;
    tvins.hInsertAfter = hPrev;

    if (nLevel == 1)
        tvins.hParent = TVI_ROOT;
    else if (nLevel == 2)
        tvins.hParent = hPrevRootItem;
    else
        tvins.hParent = hPrevLev2Item;
    // hPrev = (HTREEITEM)SendMessage(TVM_INSERTITEM, 0, (LPARAM)(LPTVINSERTSTRUCT)&tvins);
    hPrev = TreeView_InsertItem(GetSafehWnd(), &tvins);
    if (hPrev == NULL)
        return NULL;
    if (nLevel == 1)
        hPrevRootItem = hPrev;
    else if (nLevel == 2)
        hPrevLev2Item = hPrev;

    return hPrev;
    /*
    HTREEITEM hti;
    if (nLevel > 1) {
        hti = TreeView_GetParent(this->GetSafehWnd(), hPrev);
        tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
        tvi.hItem = hti;
        tvi.iImage = NULL;
        tvi.iSelectedImage = NULL;
        TreeView_SetItem(this->GetSafehWnd(), &tvi);
    }
    */
}




